package com.zx.service.impl;

import com.zx.core.page.PageInfo;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.model.Course;
import com.zx.param.CourseParam;
import com.zx.repository.CourseCustomRepository;
import com.zx.repository.CourseRepository;
import com.zx.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * <p>
 * description: 课程服务接口实现 <br>
 * create: 2024-10-25 11:59
 * </p>
 *
 * @author zhou  xun
 */
@Service
@Slf4j
public class CourseServiceImpl implements CourseService {
    private final CourseRepository courseRepository;
    private final CourseCustomRepository courseCustomRepository;

    @Autowired
    public CourseServiceImpl(CourseRepository courseRepository, CourseCustomRepository courseCustomRepository) {
        this.courseRepository = courseRepository;
        this.courseCustomRepository = courseCustomRepository;
    }


    /**
     * 保存课程
     *
     * @param param 课程信息
     */
    @Override
    public ResponseWrapper saveCourse(CourseParam param) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            Course course = getCourse(param);
            //参数验证
            //业务验证
            courseRepository.save(course);
        } catch (Exception e) {
            log.error("保存课程失败：{}", e.getMessage());
            result.fail("保存课程失败!");
        }
        return result;
    }

    /**
     * 根据课程id删除课程
     *
     * @param id 课程id
     */
    @Override
    public ResponseWrapper deleteCourseById(Long id) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            courseRepository.deleteById(id);
        } catch (Exception e) {
            log.error("删除课程失败：{}", e.getMessage());
            result.fail("删除课程失败!");
        }
        return result;
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param id 课程id
     * @return 课程信息
     */
    @Override
    public ObjectWrapper<Course> findCourseById(Long id) {
        ObjectWrapper<Course> result = new ObjectWrapper<>();
        try {
            courseRepository.findById(id).ifPresent(result::setData);
        } catch (Exception e) {
            log.error("查询课程失败：{}", e.getMessage());
            result.fail("查询课程失败!");
        }
        return result;
    }

    /**
     * 根据课程id更新课程信息
     *
     * @param param 课程信息
     */
    @Override
    public ResponseWrapper updateCourseById(CourseParam param) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            Course course = getCourse(param);
            courseRepository.save(course);
        } catch (Exception e) {
            log.error("更新课程失败：{}", e.getMessage());
            result.fail("更新课程失败!");
        }
        return result;
    }

    /**
     * 分页查询课程信息
     *
     * @param param 查询条件
     * @return {@link PageWrapper<Course>}
     * @author zhou  xun
     * @since 2024-10-25
     */
    @Override
    public PageWrapper<Course> findCourseByPage(CourseParam param) {
        PageWrapper<Course> result = new PageWrapper<>();
        try {
            // 分页信息
            PageInfo pageInfo = new PageInfo(param.getPageNum(), param.getPageSize());
            //查询分页数据
            PageRequest pageable = PageRequest.of(pageInfo.getPageNum().intValue(), pageInfo.getPageSize().intValue());
            Course course = getCourse(param);
            Page<Course> courseRepositoryByPage = courseCustomRepository.courseRepositoryByPage(course, pageable);
            if (courseRepositoryByPage != null) {
                result.setData(courseRepositoryByPage.getContent());
                result.setTotal(courseRepositoryByPage.getTotalElements());
                result.setCurrentPageNum((long) courseRepositoryByPage.getNumber());
                result.setPageSize((long) courseRepositoryByPage.getSize());
                result.setTotalPage((long) courseRepositoryByPage.getTotalPages());
            }
        } catch (Exception e) {
            log.error("分页查询课程失败：{}", e.getMessage());
            result.fail("分页查询课程失败!");
        }
        return result;
    }

    private Course getCourse(CourseParam param) {
        // 使用Lombok建造者模式构建查询条件
        return Course.builder()
                .id(param.getId())
                .courseName(param.getCourseName())
                .courseType(param.getCourseType())
                .courseIcon(param.getCourseIcon())
                .courseIntro(param.getCourseIntro())
                .build();
    }


}
